import { defineMock } from "./base";
import Mock, { N } from "mockjs";

interface params {
  rowsNumber?: number;
  total?: number;
  id?: number;
  userName?: string;
  realName?: string;
  gender?: 0 | 1 | 2;
  grade?: number;
}

const UsersList = (data: params) => {
  return Mock.mock({
    "total|100-300": 300,
    "rows|10": [
      {
        "id|+1": 1,
        userName: data.userName || "@string('lower', 6)",
        realName: data.realName || "@cname",
        password: "@string(7, 10)",
        "gender|1-2": 2,
        picture:
          "https://foruda.gitee.com/images/1723603502796844527/03cdca2a_716974.gif?imageView2/1/w/80/h/80",
        "grade|0-300": 300,
        "isAdmin|1-2": 2,
      },
    ],
  });
};

const Users = (data: params) => {
  return Mock.mock([
    {
      id: data.id,
      userName: "@string('lower', 6)",
      realName: "@cname",
      // password: "@string(7, 10)",
      "gender|1-2": 2,
      picture:
        "https://foruda.gitee.com/images/1723603502796844527/03cdca2a_716974.gif?imageView2/1/w/80/h/80",
      "grade|0-300": 300,
      isAdmin: 1,
    },
  ]);
};

export default defineMock([
  {
    url: "users",
    method: ["GET"],
    body: ({ query, params }) => {
      return {
        code: 1,
        data: UsersList({ ...query }),
        msg: "success",
      };
    },
  },
  {
    url: "users",
    method: ["PUT", "POST"],
    body: {
      code: 1,
      data: null,
      msg: "success",
    },
  },
  {
    url: "users/:id",
    method: ["DELETE"],
    body: ({ query, params }) => {
      // console.log(query);
      return {
        code: 1,
        data: null,
        msg: "success",
      };
    },
  },
  {
    url: "users/:id",
    method: ["GET"],
    body: ({ query, params }) => {
      return {
        code: 1,
        data: Users({
          ...params,
        }),
        msg: "success",
      };
    },
  },
  {
    url: "upload",
    method: ["POST"],
    body: {
      code: 1,
      data: "https://gitee.com/static/images/logo-black.svg?t=158106664",
      msg: "success",
    },
  },
]);
